package netGaming;

import gameBasicElements.*;
import gameEngine.GameStatus;

import java.io.*;

/**
 * @author Wojciech Łopata
 *
 */

public class ClientListener extends Thread
{
	private final ObjectInputStream input;
	private final NetGame netGame;
	public ClientListener(InputStream inputStream, NetGame netGame) throws IOException
	{
		setDaemon(true);
		this.input = new ObjectInputStream(inputStream);
		this.netGame = netGame;
	}
	
	@Override
	public void run()
	{
		while(!netGame.gameFinished())
		{
			try
			{
				switch((ServerMessageType) input.readObject())
				{
					case GAME_BEGIN_INFO:
						netGame.startGame((PlayerColor) input.readObject(), (TransportableOneTurnGameInfo) input.readObject());
						break;
						
					case GAME_STATUS:
						TransportableOneTurnGameInfo newGameStatus = (TransportableOneTurnGameInfo) input.readObject();
						netGame.updateStaus(newGameStatus);
						break;
						
					case FINAL_SCORES:
						ScoreBoard finalScores = (ScoreBoard) input.readObject();
						netGame.endGame(finalScores);
						break;
						
					case MOVE_REQUEST:
						netGame.sendMoveInfo();
						break;
				}
			} catch (Exception e)
			{
				netGame.breakGame(GameStatus.LISTENER_INTRRUPTION);
				break;
			}
		}
	}
}
